package com.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.model.Student;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018-8-14 0014.
 */
public class JsonToEntity {

    public static void main(String[] args) {
        String jsonData = "[{\"userName\":\"汤姆\",\"userAge\":\"23\",\"userSex\":\"男\",\"single\":\"是\"},{\"userName\":\"杰瑞\",\"userAge\":\"22\",\"userSex\":\"男\",\"single\":\"否\"},{\"userName\":\"莉莉\",\"userAge\":\"22\",\"userSex\":\"女\",\"single\":\"否\"},{\"userName\":\"汉克斯\",\"userAge\":\"22\",\"userSex\":\"男\",\"single\":\"否\"}]";
        JSONArray jsonArray = JSON.parseArray(jsonData);
        ArrayList<Student> arrayList = JSON.parseObject(jsonData, new TypeReference<ArrayList<Student>>() {});

        String jsonDataOne = "{\"userName\":\"汤姆\",\"userAge\":\"23\",\"userSex\":\"男\",\"single\":\"是\"}";
        Student studentOne = JSON.parseObject(jsonDataOne, new TypeReference<Student>() {});

        System.out.println(studentOne);

        List<Student> arrayList2 = JSON.parseArray(jsonData, Student.class);

        Iterator<Student> iterator = arrayList2.iterator();
        while (iterator.hasNext()) {
            Student student1 = iterator.next();
            System.out.println(student1.getUserName());
            System.out.println(student1.getUserAge());
            System.out.println(student1.getUserSex());
            System.out.println(student1.getSingle());
        }

        System.out.println("\r\n");

        for (Student student : arrayList) {
            System.out.println(student.getUserName());
            System.out.println(student.getUserAge());
            System.out.println(student.getUserSex());
            System.out.println(student.getSingle());
        }
    }

}
